package org.keyroy.map.core.animation;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.keyroy.map.core.manages.ResourceManage;
import org.keyroy.map.core.sprite.Sprite;
import org.keyroy.mapEditor.ui.beans.MapUtil;
import org.keyroy.util.tag.Tag;
import org.keyroy.util.tag.XmlObject;

public class AnimationGroup extends XmlObject {
	public Hashtable<String, Animation> animations = new Hashtable<String, Animation>();
	public Animation current;

	public void play(String name) {
		current = animations.get(name);
		if (current != null) {
			current.reset();
		}
	}

	public int next() {
		if (current != null) {
			return current.next();
		}
		return Sprite.SPRITE_DELETE_ID;
	}

	public void reset() {
		if (current != null) {
			current.reset();
		}
	}

	@Override
	public void initTag(Tag tag) {
		Enumeration<String> keys = animations.keys();
		String animations = MapUtil.strings(keys);
		tag.addParameter("animations", animations, animations != null);
	}

	@Override
	public void initObj(Tag tag) {
		animations.clear();
		String data = tag.getParameterValue("animations");
		if (data != null) {
			List<String> strings = new ArrayList<String>();
			MapUtil.strings(strings, data);
			for (int i = 0; i < strings.size(); i++) {
				Animation animation = ResourceManage.getAnimation(strings.get(i));
				if (animation != null) {
					animations.put(animation.name, animation);
				}
			}
		}
	}

}
